package javaweb.wudu.zengismall.controller;

import com.github.pagehelper.Page;
import javaweb.wudu.zengismall.dataBean.Computer;
import javaweb.wudu.zengismall.javaBaen.ItemPage;
import javaweb.wudu.zengismall.service.PaginationSelectComputerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@CrossOrigin(origins = "*")
@ResponseBody
@RequestMapping("/items")
public class ItemController {

    @Autowired
    private PaginationSelectComputerService computerService;

    @RequestMapping("/getAll")
    public ItemPage selectAll(int pageNumber, int pageSize){
        System.out.println(pageNumber + " " + pageSize);
        Page<Computer> page = computerService.selectAll(pageNumber, pageSize);
        ItemPage itemPage = new ItemPage();
        itemPage.setPageNumber(pageNumber);
        itemPage.setPageSum(page.getPages());
        itemPage.setPageSize(pageSize);
        itemPage.setComputers(page.getResult());
        return itemPage;
    }

    @RequestMapping("/ByPrice")
    public ItemPage selectByPrice(float min,float max,int pageNumber, int pageSize){
        System.out.println(min + " " + max + " " +pageNumber + " " + pageSize);
        Page<Computer> page = computerService.selectByPrice(min,max,pageNumber, pageSize);
        ItemPage itemPage = new ItemPage();
        itemPage.setPageNumber(pageNumber);
        itemPage.setPageSum(page.getPages());
        itemPage.setPageSize(pageSize);
        itemPage.setComputers(page.getResult());
        return itemPage;
    }
}
